package com.moss.cloud.base.biz.listener;
import com.moss.cloud.base.biz.operation.flow.BusStateOperation;
import com.moss.cloud.base.biz.operation.flow.factory.BusStateFactory;
import com.moss.cloud.common.core.exception.BaseException;
import com.moss.cloud.common.core.exception.SystemErrorType;
import com.moss.cloud.common.flow.event.TaskEvent;
import com.moss.cloud.common.flow.event.TaskEventListener;
import com.moss.cloud.common.flow.model.NodeTask;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class FlowTaskListener implements TaskEventListener {

    @Override
    public void onApplicationEvent(TaskEvent event) {
        NodeTask nodeTask = event.getNodeTask();
        BusStateOperation operation = BusStateFactory.stateType(nodeTask.getStatus())
                .orElseThrow(() -> new BaseException(SystemErrorType.TASK_STATUS_MATCH));
        operation.stateProcess(nodeTask);
    }
}
